<!-- 友链编辑弹窗 -->
<template>
  <el-dialog :title="isUpdate ? '修改消息推送' : '添加消息推送'" :visible="visible" width="750px" :destroy-on-close="true"
    :lock-scroll="false" :close-on-click-modal="false" :before-close="confirmClose" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :sm="12">
          <!-- <el-form-item label="消息用户:" prop="member_id">
            <el-select clearable remote filterable :loading="loading" class="ele-block" v-model="form.member_id"
              placeholder="请选择消息用户" :remote-method="remoteMethod">
              <el-option label="-1 -- 全局消息" :value="-1" />
              <el-option v-for="(v, i) in itemlist" :key="i" :value="`${v.id}`" :label="`${v.id} -- ${v.nickname}`" />
              <el-pagination small layout="prev, pager, next" @current-change=remoteMethod :hide-on-single-page="true"
                :page-size=pageSize :current-page=currentPage :page-count=pageCount :total=pageTotal />
            </el-select>
          </el-form-item> -->
          <el-form-item label="消息用户:" prop="member_id">
            <PageSelect :placeholder="'请选择消息用户'" v-model="form.member_id" :whereName="'nickname'"
              :url="'/BoxNotice/member_info'" :optionLable= "'nickname'" :optionvalue="'id'" :multiple="false" />
          </el-form-item>
          <el-form-item label="通知标题:" prop="title">
            <el-input clearable :maxlength="20" v-model="form.title" placeholder="请输入通知标题" />
          </el-form-item>
        </el-col>

        <el-col :sm="12">
          <el-form-item label="消息类型:" prop="source">
            <el-select clearable class="ele-block" v-model="form.source" placeholder="请选择消息类型">
              <el-option label="系统公告" :value="1" />
              <el-option label="评论消息" :value="2" />
              <el-option label="限制消息" :value="3" />
              <el-option label="签到消息" :value="4" />
              <el-option label="审核通过" :value="5" />
              <el-option label="审核失败" :value="6" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="系统ID:" prop="system_id">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.system_id"
              placeholder="请输入系统ID"
            />
          </el-form-item> -->
        </el-col>
      </el-row>
      <el-form-item label="通知内容:" prop="content" style="margin-top: 20px">
        <el-input :rows="3" clearable type="textarea" :maxlength="200" v-model="form.content" placeholder="请输入通知内容" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";
import PageSelect from "@/components/pageSelect/index.vue";

export default {
  name: "NoticEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: { PageSelect },
  data() {
    return {
      // 表单数据
      form: Object.assign(
        {
          member_id: "",
          source: "",
          title: "",
          content: "",
          // system_id: ""
        },
        this.data,

      ),
      // 表单验证规则
      rules: {
        member_id: [
          { required: true, message: "请选择消息用户", trigger: "blur" }
        ],
        source: [
          { required: true, message: "请选择消息类型", trigger: "blur" }
        ],
        title: [{ required: true, message: "请输入消息标题", trigger: "blur" }],
        content: [
          { required: true, message: "请输入消息内容", trigger: "blur" }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,

      //分页功能
      pageSize: 15, //每页显示条数  6条刚好
      currentPage: 1, //当前页数
      pageCount: 6, //按钮数，超过时会折叠
      pageTotal: 0, //总条数
      itemlist: [],
      queryParams: {},
      query:''
    };
  },

  created() {
    axios.post("/BoxNotice/member_info", { page: this.currentPage, limit: this.pageSize }).then(res => {
      if (res.data.code != 0) {
        console.log('获取数据失败！')
        return
      }
      this.itemlist = res.data.data;
      this.pageTotal = res.data.count
    });
  },

  watch: {
    visible(val) {
      if (val) {
        this.remoteMethod()
      }
    },
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/BoxNotice/sendNotice", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    changeNumber(val) {
      if(this.query){
        this.queryParams = { page: val, limit: this.pageSize, nickname: this.query };
      }else{
        this.queryParams = { page: val, limit: this.pageSize }
      }
    
      axios.post("/BoxNotice/member_info", this.queryParams).then(res => {
        this.itemlist = res.data.data;
      });
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          if (!this.isUpdate) {
            this.form = {};
          }
        })
        .catch(() => { });
    },
    remoteMethod(query) {
      if (query == '') {
        this.query = ''
        this.queryParams = { page: this.currentPage, limit: this.pageSize }
      } else {
        this.query = query;
        this.queryParams = { page: this.currentPage, limit: this.pageSize, nickname: query };
      }
      axios.post("/BoxNotice/member_info", this.queryParams).then(res => {
        if (res.data.code != 0) {
          console.log('获取数据失败！')
          return
        }
        this.itemlist = res.data.data;
        this.pageTotal = res.data.count
      });
    },
  }
};
</script>

<style scoped></style>
